package com.fr.data.core.db.dml;

import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.data.core.db.dialect.Dialect;
import com.fr.data.core.db.dialect.DialectFactory;
import com.fr.data.core.db.dialect.SybaseDialect;
import com.fr.data.core.db.tableObject.Column;
import com.fr.data.core.db.tableObject.ColumnSize;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CreateTable extends AbstractDML
{
  private List columnsList;
  private String checkFilter;
  private String foreignReference;

  public CreateTable(Table paramTable)
  {
    this(paramTable, DialectFactory.getDefaultDialect());
  }

  public CreateTable(Table paramTable, Dialect paramDialect)
  {
    super(paramTable, paramDialect);
    this.columnsList = new ArrayList();
    this.checkFilter = null;
    this.foreignReference = null;
  }

  public void addColumn(Column paramColumn)
  {
    this.columnsList.add(paramColumn);
  }

  public void clearColumn()
  {
    this.columnsList.clear();
  }

  public Column getColumn(int paramInt)
  {
    return ((Column)this.columnsList.get(paramInt));
  }

  public int getColumnSize()
  {
    return this.columnsList.size();
  }

  public void addColumn(String paramString, int paramInt1, int paramInt2)
  {
    addColumn(paramString, paramInt1, paramInt2, Boolean.valueOf(false), null, Boolean.valueOf(false), Boolean.valueOf(false));
  }

  public void addColumn(String paramString1, int paramInt1, int paramInt2, Boolean paramBoolean1, String paramString2, Boolean paramBoolean2)
  {
    addColumn(paramString1, paramInt1, paramInt2, paramBoolean1, paramString2, paramBoolean2, Boolean.valueOf(false));
  }

  public void addColumn(String paramString, int paramInt1, int paramInt2, Boolean paramBoolean1, Object paramObject, Boolean paramBoolean2, Boolean paramBoolean3)
  {
    Column localColumn = new Column();
    localColumn.setName(paramString);
    localColumn.setValueType(paramInt1);
    localColumn.setColumnSize(new ColumnSize(paramInt2));
    localColumn.setAllowNull(!(paramBoolean1.booleanValue()));
    localColumn.setDefaultValue(paramObject);
    localColumn.setPrimaryKey(paramBoolean3.booleanValue());
    localColumn.setSelfIncrease(paramBoolean2.booleanValue());
    addColumn(localColumn);
  }

  public PreparedStatement createPreparedStatement(Connection paramConnection)
    throws SQLException
  {
    if ((paramConnection == null) || (getTable() == null) || (this.dialect == null))
      return null;
    String str = toStatementSQLString(this.dialect, paramConnection);
    FRContext.getLogger().log(Level.INFO, str.toString());
    System.out.println(str);
    PreparedStatement localPreparedStatement = paramConnection.prepareStatement(str);
    return localPreparedStatement;
  }

  public String toStatementSQLString(Dialect paramDialect, Connection paramConnection)
  {
    if (getTable() == null)
      return null;
    if (this.columnsList.isEmpty())
      return null;
    String str1 = getTable().toStatementSQLString(paramDialect);
    StringBuffer localStringBuffer = new StringBuffer("CREATE TABLE ");
    localStringBuffer.append(str1).append("(");
    int i = 0;
    int j = this.columnsList.size();
    while (i < j)
    {
      localStringBuffer.append(getColumn(i).toSQL(paramDialect, paramConnection, str1));
      if (i < j - 1)
        localStringBuffer.append(",");
      ++i;
    }
    if (!(paramDialect instanceof SybaseDialect))
    {
      str2 = createPrimaryKeySQL();
      if (StringUtils.isNotBlank(str2))
        localStringBuffer.append(",").append(str2);
    }
    String str2 = createForeignKeySQL();
    if (StringUtils.isNotBlank(str2))
      localStringBuffer.append(",").append(str2);
    String str3 = createCheckSQL();
    if (StringUtils.isNotBlank(str3))
      localStringBuffer.append(",").append(str3);
    localStringBuffer.append(")");
    FRContext.getLogger().log(Level.INFO, localStringBuffer.toString());
    return localStringBuffer.toString();
  }

  public String getCheckFilter()
  {
    return this.checkFilter;
  }

  public void setCheckFilter(String paramString)
  {
    this.checkFilter = paramString;
  }

  public String getForeignReference()
  {
    return this.foreignReference;
  }

  public void setForeignReference(String paramString)
  {
    this.foreignReference = paramString;
  }

  private String createCheckSQL()
  {
    if (StringUtils.isBlank(getCheckFilter()))
      return "";
    return "CHECK " + getCheckFilter();
  }

  private String createPrimaryKeySQL()
  {
    ArrayList localArrayList = new ArrayList();
    int i = 0;
    int j = getColumnSize();
    while (i < j)
    {
      if (getColumn(i).isPrimaryKey())
        localArrayList.add(getColumn(i));
      ++i;
    }
    if (localArrayList.size() < 1)
      return "";
    StringBuffer localStringBuffer = new StringBuffer("PRIMARY KEY (");
    j = 0;
    int k = localArrayList.size();
    while (j < k)
    {
      localStringBuffer.append(this.dialect.column2SQL(((Column)localArrayList.get(j)).getName()));
      if (j < k - 1)
        localStringBuffer.append(",");
      ++j;
    }
    localStringBuffer.append(")");
    return localStringBuffer.toString();
  }

  private String createForeignKeySQL()
  {
    ArrayList localArrayList = new ArrayList();
    int i = 0;
    int j = getColumnSize();
    while (i < j)
    {
      if (getColumn(i).isForeignKey())
        localArrayList.add(getColumn(i));
      ++i;
    }
    if (localArrayList.size() < 1)
      return "";
    StringBuffer localStringBuffer = new StringBuffer("FOREIGN KEY (");
    j = 0;
    int k = localArrayList.size();
    while (j < k)
    {
      localStringBuffer.append(this.dialect.column2SQL(((Column)localArrayList.get(j)).getName()));
      if (j < k - 1)
        localStringBuffer.append(",");
      ++j;
    }
    localStringBuffer.append(") REFERENCES ");
    localStringBuffer.append(getForeignReference());
    return localStringBuffer.toString();
  }
}